package com.yuandian.management.service.impl;


import com.yuandian.api.management.entity.ConfArea;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.mapper.DataCenterMapper;
import com.yuandian.management.service.IDataCenterAreaService;
import com.yuandian.management.vo.*;
import com.yuandian.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据中心区域
 *
 * @author zhoutian
 * @date 2021/1/5 17:18
 */
@Service
public class DataCenterAreaServiceImpl implements IDataCenterAreaService {
    @Autowired
	DataCenterMapper dataCenterMapper;

    /**
     * 根据条件分页查询数据中心区域信息列表
     *
     * @param params 参数信息
     * @return PageDataInfo
     */
    @Override
    public List<String> selectAreaIdList(DataCenterParams params) {
        return dataCenterMapper.selectAreaIdList(params);
    }

    /**
     * 根据条件分页查询区域列表
     *
     * @param params 参数
     * @return PageDataInfo
     */
    @Override
    public List<Map<String, Object>> selectAreaList(DataCenterParams params) {
        return dataCenterMapper.selectAreaList(params);
    }


    /**
     * 根据ID查询数据中心区域信息
     *
     * @param id 参数ID
     * @return DataCenterInfo
     */
    @Override
    public DataCenterInfo selectDataCenterAreaById(Integer id) {
        return dataCenterMapper.selectDataCenterArea(id);
    }

    /**
     * 修改数据中心区域信息
     *
     * @param params 参数信息
     * @return Result
     */
    @Override
    public R updateDataCenterArea(DataCenterInfo params) {
        params.setUpdateBy(SecurityUtils.getUser().getName());
        int row = dataCenterMapper.updateDataCenterArea(params);
        if (row > 0) {
            return R.ok();
        }
        return R.failed();
    }


    /**
     * 校验名称称是否唯一
     *
     * @param areaId   区域ID
     * @param centerId 数据中心ID
     * @return boolean
     */
    @Override
    public boolean checkAreaUnique(Integer areaId, Integer centerId) {
        Integer selectId = dataCenterMapper.checkAreaUnique(areaId, centerId);
        if (StringUtils.isNotNull(selectId)) {
           return false;
        }
        return true;
    }

    /**
     * 新增数据中心区域信息
     *
     * @param params 参数
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R insertDataCenterArea(DataCenterAreaInfo params) {
        try {
            DataCenterParams dataCenterParams = new DataCenterParams();
            List<DataCenterArea> insertDataList = new ArrayList<>();
            dataCenterParams.setCenterId(params.getCenterId());
            //获取当前已分配的区域
            List<String> areaId = dataCenterMapper.selectAreaIdList(dataCenterParams);
            List<String> removeList = new ArrayList<>(areaId);
            removeList.removeAll(params.getAreaIds());
            //1.需要新增的数据集
            List<String> insertList = new ArrayList<>(params.getAreaIds());
            insertList.removeAll(areaId);
            //获取区域信息
            if (!insertList.isEmpty()) {
                List<ConfArea> insertAreaList = dataCenterMapper.selectAreaById(insertList);
                String username = SecurityUtils.getUser().getName();
                //循环获得新增数据集
                DataCenterArea data;
                int i = 1;
                for (ConfArea area : insertAreaList) {
                    data = new DataCenterArea();
                    data.setAreaParentId(area.getParentId());
                    data.setCenterId(params.getCenterId());
                    data.setAreaId(area.getId());
                    data.setSequence(i++);
                    data.setCreateBy(username);
                    data.setUpdateBy(username);
                    insertDataList.add(data);
                }
                if (insertAreaList.size() > 0){
					dataCenterMapper.insertDataCenterArea(insertDataList);
				}
            }
            //2.需要删除的数据集
            Integer[] removeIds = new Integer[removeList.size()];
            int j = 0;
            for (String removeId : removeList) {
                removeIds[j++] = Integer.parseInt(removeId);
            }
            if (removeIds.length > 0) {
                dataCenterMapper.deleteAreaByCenterIdAndAreaIds(removeIds, params.getCenterId());
            }
        } catch (Exception e) {
        	e.printStackTrace();
            return R.failed();
        }
        return R.ok();
    }

    @Override
    public R deleteDataCenterAreaByIds(Integer[] ids) {
        int row = dataCenterMapper.deleteDataCenterAreaByIds(ids);
        if (row > 0) {
            return R.ok();
        }
        return R.failed();
    }
}
